package com.hengpeng.msls.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.util.StringHelper;
import com.hengpeng.itfinbase.constants.TransCodeConstants;
import com.hengpeng.itfinbase.message.ResponseMessage;
import com.hengpeng.itfinbase.message.partner.PartnerTransType;
import com.hengpeng.itfinbase.message.partner.PayRequestMsg;
import com.hengpeng.itfinbase.persist.Account;
import com.hengpeng.itfinbase.persist.UserInfo;
import com.hengpeng.msls.vo.SessionMember;

/**
 * <pre>
 * 充值
 * </pre>
 * 
 * @author shenshaopeng
 * @version 1.0, 2014-1-10
 */
@Controller
@RequestMapping("/pay")
public class PaymentController extends BaseController {

    /**
     * 充值页面
     */
    @RequestMapping(value = "/toimprest")
    public String goImprest(HttpServletRequest request, ModelMap map) {
        SessionMember session = getSessionMember(request);
        if (session == null) {
            return "/page/login/index.jsp";
        }
        UserInfo user = serviceFactory.getUserInfoService().getUserInfoById(session.getUser().getId());
        if (StringHelper.isEmpty(user.getTrustAccountId())){
        	return "redirect:/member/goregpnr.htm";
        }
        Account account = serviceFactory.getAccountService().getAccount(session.getUser().getId());
        map.put("account", account);
        return "/page/account/imprest.jsp";
    }

    /**
     * 充值
     * 
     * @param request
     * @param money
     * @return
     */
    @RequestMapping(value = "/imprest", method = RequestMethod.POST)
    @ResponseBody
    public ServiceResult<ResponseMessage> imprest(HttpServletRequest request, String money) {
        ServiceResult<ResponseMessage> result = new ServiceResult<ResponseMessage>();

        PayRequestMsg reqMsg = new PayRequestMsg();
        UserInfo user = getSessionMember(request).getUser();
        UserInfo m = serviceFactory.getUserInfoService().getUserInfoById(user.getId());
        reqMsg.setUserId(m.getId());
        reqMsg.setTransAmt(money);
        reqMsg.setPartnerTransType(PartnerTransType.NetSave);
        reqMsg.setTransType(TransCodeConstants.PartnerTransCode.REQUEST);
        reqMsg.setPageType("2");//app应用风格
        reqMsg.setBgRetUrl(service_notify_url);
        reqMsg.setRetUrl(page_return_url);
        ResponseMessage response = (ResponseMessage) serviceFactory.getProtFactory().remoteTrans(reqMsg);
        result.setReturnValue(response);

        return result;
    }

}
